<?php

class SetTitleAction extends CAction
{
    public function run()
    {
        if(!isset($_POST['value']) || !isset($_GET['id']))
            throw new CHttpException(400,'the request is invalid');
                
        $recipe=Recipe::loadObjectById($_GET['id'],array('title','userId'));
        if($recipe===null)
            throw new CHttpException(404,'The requested recipe does not exist.');
        if($recipe->userId!=Yii::app()->user->id)
            throw new CHttpException(403,'cannot edit the recipe not belong to you');

        $succ=false;
        if($_POST['value']===$recipe->title){
            $succ=true;
        }
        else {
            $oldTitle=$recipe->title;
            $recipe->title=$_POST['value'];
            $succ=Recipe::saveObject($recipe,array('title','updateTime'));
            if(!$succ)
                $recipe->title=$oldTitle;
        }

        $this->controller->renderPartial('/common/editInPlace',array(
            'succ'=>$succ, 
            'data'=>$recipe->title,
            'error'=>$recipe->getError('title'),
        ));
    }
}

?>